package com.kuaimacode.kframework.admin.services.role;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.permission.SysAgentPermissonService;
import com.kuaimacode.kframework.mybatis.mapper.agent.SysAgentMapRolePermissionMapper;
import com.kuaimacode.kframework.mybatis.mapper.agent.SysAgentRoleMapper;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentPermission;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentRole;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色信息
 */
@Service
public class SysAgentRoleService extends ServiceImpl<SysAgentRoleMapper, SysAgentRole> {


	@Autowired
	private SysAgentMapRolePermissionMapper sysMapRolePermissionMapper;
	@Autowired
    private SysAgentPermissonService agentPermissonService;
	
	
	/**
	 * 新建角色
	 * @param role
	 */
	public Long createRole(SysAgentRole role) {
		baseMapper.insert(role);
		return role.getRoleId();
	}
	
	/**
	 * 分页查找所有的角色
	 *
	 * @param pageNum  当前页码，从1开始
	 * @param pageSize 一页显示多少行
	 * @return
	 */
	public PageInfo<SysAgentRole> findAll(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<SysAgentRole> roles = baseMapper.selectList(null);
		return new PageInfo<SysAgentRole>(roles);
	}

	public int countByAgentId(String agentId) {
        QueryWrapper<SysAgentRole> wrapper = new QueryWrapper<>();
        wrapper.eq("agent_id", agentId);
        return baseMapper.selectCount(wrapper);
    }

    public SysAgentRole getSmallRole(String agentId) {
        QueryWrapper<SysAgentRole> wrapper = new QueryWrapper<>();
        wrapper.eq("agent_id", agentId);
        wrapper.orderByDesc("role_id");
        PageHelper.startPage(1, 1);
        return baseMapper.selectOne(wrapper);
    }

	/**
	 * 给角色分配权限
	 *
	 * @param id          角色ID
	 * @param permissionIds 权限ids
	 */
	public void grant(String id, String[] permissionIds) {
		sysMapRolePermissionMapper.grant(id,permissionIds);
	}

    /**
     * 为角色分配所有权限
     * @param roleId
     */
	public void grantAllPermission(String roleId) {
	    List<SysAgentPermission> permissions = agentPermissonService.list();
	    if (CollectionUtils.isNotEmpty(permissions)) {
	        String[] strings = new String[permissions.size()];
	        sysMapRolePermissionMapper.grant(roleId, permissions.stream().map(permission ->
                    permission.getPermissionId().toString()).collect(Collectors.toList()).toArray(strings));
        }
    }
	
	/**
	 * 删除角色绑定的全部权限
	 * @param role_id
	 */
	public void removeAllPermissionByUserId(String role_id) {
        
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("role_id", role_id);
		
		sysMapRolePermissionMapper.deleteByMap(columnMap);
	}
}

